// @ts-check
if ('serviceWorker' in navigator) {
  navigator.serviceWorker
    .register('/workbox/build.sw.js')
    .then((reg) => {
      reg.onupdatefound = () => {
        const installingWorker = reg.installing;
        // @ts-ignore
        installingWorker.onstatechange = () => {
          // @ts-ignore
          switch (installingWorker.state) {
            case 'installed':
              if (navigator.serviceWorker.controller) {
                // 触发 CustomEvent
                const event = document.createEvent('Event');
                event.initEvent('sw.update', true, true);
                window.dispatchEvent(event);
              }
              break;
          }
        };
      };
    })
    .catch((e) => {
      console.error('Error during service worker registration:', e);
    });
}
